//
//  CardQueue.m
//  MagicCardDemo
//
//  Created by 向恒 on 15/7/5.
//  Copyright (c) 2015年 向恒. All rights reserved.
//

#import "CardQueue.h"

@implementation CardQueue
///为了归档方便使用（看到的一个例子），才加了等号及后面。
@synthesize arrCards = _arrCards;
@synthesize nCostTime = _nCostTime;
@synthesize nScore = _nScore;
@synthesize date = _date;

-(id)init
{
    if(self == [super init])
    {
        NSLog(@"%s","init CardQueue.");
        if(self.arrCards == nil)
        {
            self.arrCards = [[NSMutableArray alloc]initWithObjects:0];
        }
        //[self ShuffleCards];
    }
    return  self;
}

-(void)ClearData
{
    [self.arrCards removeAllObjects];
    self.nCostTime = 0;
    self.nScore = 0;
    self.date = @"";
}

-(void)ShuffleCards
{

    int heap[52] = {};
    
    for(int i = 0;i < 52;i++)
    {
        heap[i] = i;
    }
    
    for(int i = 51;i >=0
        ;i--)
    {
        int n= 0;
        if(i == 0)
            n = 0;
        else
            n = arc4random()%i;
        int tmp;
        tmp = heap[i];
        heap[i] = heap[n];
        heap[n] = tmp;
        
        @try {
            NSNumber* num = [NSNumber numberWithInt:heap[i]];
            [self.arrCards addObject:num];
        }
        @catch (NSException *exception) {
            NSLog(@"%@",exception.description);
        }

    }
    
}


//自定义类归档需要实现的两个方法。
//归档
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_arrCards forKey:@"arrCards"];
    [aCoder encodeInt:_nCostTime forKey:@"nCostTime"];
    [aCoder encodeInt:_nScore forKey:@"nScore"];
    [aCoder encodeObject:_date forKey:@"date"];
}

//解档
-(id)initWithCoder:(NSCoder *)aDecoder
{
    _arrCards = [aDecoder decodeObjectForKey:@"arrCards"];
    _nCostTime = [aDecoder decodeIntForKey:@"nCostTime"];
    _nScore = [aDecoder decodeIntForKey:@"nScore"];
    _date = [aDecoder decodeObjectForKey:@"date"];
    return self;
}


//说是深拷贝需要的方法，但是这里没有中断过。
- (id)copyWithZone:(NSZone * )zone
{
    CardQueue* q= [[[self class]allocWithZone:zone]init];
    q.arrCards = [self.arrCards copyWithZone:zone];
    q.nCostTime = self.nCostTime;
    q.nScore = self.nScore;
    q.date = [[[self class]allocWithZone:zone]init];
    return self;
}

@end
